<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Muser extends CI_Model
	{
		function __construct()
		{
			//parent::CI_Model();	
		}
		//验证用户是否登录
		public function check_login()
		{
                    if($this->session->userdata('admin_user'))
                    {
                        return TRUE;
                    }else{
                        return FALSE;
                    }
		}
		
		//user login
		public function login()
		{
			
                    $username = $this->input->post('username');
                    $password = $this->input->post('password');
                    $query = $this->db->query("SELECT * FROM wtt_admins WHERE username='".$username."' LIMIT 1");	
                    $re_data1 = $query->row_array();
                    if($re_data1)
                    {
			//echo $re_data1['upsw'];
			//md5($this->input->post('pwd'));
                        //echo $re_data1['password'].'<br>'.md5($this->input->post('password'));
                        //exit;
                        $DB1 = $this->load->database('update', TRUE);
			if($re_data1['password'] == md5($this->input->post('password')))
			{
				
                             $DB1->query("update wtt_admins SET errnum=0,lasttime=".time()." WHERE username='".$username."'");
                             return $re_data1;
                                        
			}else
			{
                              $DB1->query("update wtt_admins SET errnum=errnum+1 WHERE username='".$username."'");
                               return FALSE;	
				}
			}else
			{
				return FALSE;
			}
                    }
                    
                    function del($admin_id=0)
                    {
                        $DB1 = $this->load->database('update', TRUE);
                        $DB1->query("delete from  wtt_admins WHERE id=".$admin_id);
                        return TRUE;
                    }
                    function add()
                    {
                        $DB1 = $this->load->database('update', TRUE);
                        $username = $this->input->post('username');
                        $new_password = $this->input->post('password');
                        $sql_arr = array('username'=>$username,'password'=>$new_password,'lasttime'=>  time());
                        $DB1->query($DB1->insert_string('wtt_admins',$sql_arr));
                        return TRUE;
                            
                    }
                    
                    function pdel($admin_id=0){
                        $DB1 = $this->load->database('update', TRUE);
                        $DB1->query('delete from wtt_products where id='.$admin_id);
                        return true;
                    }
                    
                    function padd()
                    {
                        $p_name = $this->input->post('p_name');
                        $p_price = $this->input->post('p_price');
                        $p_jifen = $this->input->post('p_jifen');
                        $sql_arr = array(
                            'p_name' => $p_name,
                            'p_price' => $p_price,
                            'p_jifen' => $p_jifen
                        ); 
                        $DB1 = $this->load->database('update', TRUE);
                        $DB1->insert('wtt_products',$sql_arr);
                        return true;
                    }
                    function setactive($uid,$active)
                    {
                        $DB1 = $this->load->database('update', TRUE);
                        $DB1->where('wb_id', $uid);
                        $arr_sql = array('status'=>$active);
                        $DB1->update('wtt_customers',$arr_sql);
                        if($active == 1)
                        {
                            $this->_endtg($uid);
                        }
                        
                    }
                    private function _endtg($uid)
                    {
                        //$date = array('follow_status'=>0);
                        $DB1 = $this->load->database('update', TRUE);
                        $DB1->where('uid',$uid);
                        $DB1->delete('wtt_follow');
                        //$date = array('rp_status'=>0);
                        $DB1->where('uid',$uid);
                        $DB1->delete('wtt_repost');
                    }
                        
                    
	}